package cn.tedu.pet.product.controller;

import cn.tedu.pet.commons.pojo.vo.PageData;
import cn.tedu.pet.commons.web.JsonResult;
import cn.tedu.pet.product.pojo.param.OrderAddNewParam;
import cn.tedu.pet.product.pojo.vo.OrderListItemVO;
import cn.tedu.pet.product.service.IOrderService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
@Api(tags = "01.订单管理模版")
@Slf4j
@Validated
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/add-new")
    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid OrderAddNewParam orderAddNewParam){
        log.debug("开始处理【添加订单】的请求，参数为{}",orderAddNewParam);
        orderService.addNew(orderAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
    })
    public JsonResult<PageData<OrderListItemVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page){
        log.debug("开始处理【查询订单列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<OrderListItemVO> pageData = orderService.list(pageNum);
        return JsonResult.ok(pageData);
    }
}
